
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
const mongo = cloud.mongo.db
export async function mongoList(dbName:string,whereJson:object,skip:number|null,limit:number|null) {
  return new Promise((resolve, reject) => {
    let po = mongo.collection(dbName).find(whereJson)
    if (!nw.util.isNull(skip)){
      po = po.skip(skip)
    }
    if (!nw.util.isNull(limit)){
      po = po.limit(limit)
    }
    po.toArray((err, docs) => {
      if (err) {
        reject(err);
      } else {
        resolve(docs);
      }
    });
  });
};

export async function countDoc(dbName:string,whereJson:object) {
  return new Promise((resolve, reject)=>{
    mongo.collection(dbName).find(whereJson).count((error, result) => {
      if (error) {
        reject(error);
      } else {
        resolve(result);
      }
    })

  })
}

